﻿// -----------------------------------------------------------------------
// <copyright file="MouseController.cs" company="Microsoft">
//  Blah blah
// </copyright>
// -----------------------------------------------------------------------

namespace Eeeek
{
    using System.Drawing;
    using System.Threading;

    /// <summary>
    /// Controls the mouse
    /// </summary>
    public class MouseController : IMouseController
    {
        #region Fields

        private readonly int mouseButtonUpDownDelayInMilliseconds;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="MouseController"/> class.
        /// </summary>
        /// <param name="mouseButtonUpDownDelayInMilliseconds">The mouse button up down delay in milliseconds.</param>
        public MouseController(int mouseButtonUpDownDelayInMilliseconds)
        {
            this.mouseButtonUpDownDelayInMilliseconds = mouseButtonUpDownDelayInMilliseconds;
        }

        #region IMouseController Members

        /// <summary>
        /// Sends the mouse click to point.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="point">The point.</param>
        public void SendMouseClickToPoint(MouseAction action, Point point)
        {
            NativeMethods.SetCursorPos(point.X, point.Y);
            this.DoMouseAction(action);
        }

        #endregion

        /// <summary>
        /// Does the mouse action.
        /// </summary>
        /// <param name="action">The action.</param>
        private void DoMouseAction(MouseAction action)
        {
            switch (action)
            {
                case MouseAction.LeftClick:
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    Thread.Sleep(this.mouseButtonUpDownDelayInMilliseconds);
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    break;
                case MouseAction.RightClick:
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
                    Thread.Sleep(this.mouseButtonUpDownDelayInMilliseconds);
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
                    break;
                case MouseAction.LeftDoubleClick:
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    Thread.Sleep(this.mouseButtonUpDownDelayInMilliseconds);
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    Thread.Sleep(this.mouseButtonUpDownDelayInMilliseconds);
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    Thread.Sleep(this.mouseButtonUpDownDelayInMilliseconds);
                    NativeMethods.mouse_event(NativeMethods.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    break;
            }
        }
    }
}
